package stack;

//栈实现综合计算器
public class Calculator {
    public static void main(String[] args) {

        String expression = "3+2*6-2";
        //创立两个栈 一个数栈 一个符号栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        //定义需要的相关变量
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        String ch = " "; //每次扫描到就保存到ch中
        while (true) {
            ch = expression.substring(index, index + 1);
            if (operStack.isOper(ch)) {//如果是运算符
                //判断当前栈是否为空
                if (!operStack.ifEmpty()) {
                    //如果符号栈有操作符，就进行比较，如果当前操作符的优先级小于或等于栈中的操作符
                    //就需要从数栈中pop出两个数，在从符号栈中pop出一个符号，进行运算，得到结果入数栈，然后将当前操作符符符号栈
                    if (operStack.priority(ch) <= operStack.priority(String.valueOf(operStack.peek()))) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.peek();
                        res = numStack.cal(num1, num2, oper);
                        //把结果加入到数栈
                    }
                } else {
                    //为空直接入栈
                }
            }

        }


    }


}
